<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" size="large" :model="queryParams" ref="queryFormRef" :inline="true" label-width="88px">

      <el-form-item label="客户账号" prop="equipmentId">
        <el-select @change="handleQuery" class="!w-240px" clearable filterable v-model="queryParams.customerAccount"
          placeholder="请输入关键字搜索">
          <el-option v-for="dict in queryList" :key="dict.id" :label="`${dict.customerAccount} - ${dict.customerName}`"
            :value="dict.customerAccount" />
        </el-select>
      </el-form-item>

      <el-form-item label="图号" prop="drawingNumber">
        <el-input @input="handleQuery" v-model="queryParams.drawingNumber" placeholder="请输入图号" clearable
          @keyup.enter="handleQuery" class="!w-200px" />
      </el-form-item>
      <el-form-item label="品名" prop="productName">
        <el-input @input="handleQuery" v-model="queryParams.productName" placeholder="请输入品名" clearable
          @keyup.enter="handleQuery" class="!w-200px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['business:draw-config:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="warning" plain @click="handleImport">
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['business:draw-config:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['business:draw-config:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table :header-cell-style="{ background: '#e6f7ef', color: '#008c46' }" row-key="id" border v-loading="loading"
      :data="list" :stripe="true" :show-overflow-tooltip="true" @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="客户账号" align="center" prop="customerAccount" />
      <el-table-column label="客户" align="center" prop="customerName" />
      <el-table-column label="仓库地点" align="center" prop="storeLocation" />
      <el-table-column label="仓库名称" align="center" prop="storeName" />
      <el-table-column label="图号" align="center" prop="drawingNumber" />
      <el-table-column label="品名" align="center" prop="productName" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:draw-config:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:draw-config:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DrawConfigForm ref="formRef" @success="getList" />

  <!-- 导入弹窗 -->
  <importForm ref="importFormRef" @success="getList" />

</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DrawConfigApi, DrawConfig } from '@/api/business/drawconfig'
import DrawConfigForm from './DrawConfigForm.vue'
import importForm from './ImportForm.vue'


/** 图号配置 列表 */
defineOptions({ name: 'DrawConfig' })

const queryList = ref([])

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DrawConfig[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerAccount: undefined,
  customerName: undefined,
  storeLocation: undefined,
  storeName: undefined,
  drawingNumber: undefined,
  productName: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DrawConfigApi.getDrawConfigPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DrawConfigApi.deleteDrawConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除图号配置 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await DrawConfigApi.deleteDrawConfigList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: DrawConfig[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DrawConfigApi.exportDrawConfig(queryParams)
    download.excel(data, '图号配置.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  const data = await DrawConfigApi.getDrawConfigListQuery(null)

  queryList.value = data

  console.log("dataaaa", data);


  getList()
})
</script>
